var vo={};
$(function(){
	var openWindow=window.document;
	var searchHeight=$("#divform").height();
	$("#listShop").height($(openWindow).height()-$(openWindow).height()*0.05-searchHeight-10);
	$("#isflag").val("");
	$("#all").addClass("a_hover");
	//动态设置改变操作列宽度
	var fcol=opt.split('</a>');
	var contrlColumn=0;
	if(fcol.length>0){
		contrlColumn=(fcol.length-1)*fcolMinWidth;
	}
	$("#listShop").datagrid({
					url:'/shop/getShop',
					title:'用户列表',
					method:'get',
					pagination:true,
					pageList: window.PageSize,
					loadMsg:'加载中......',
					view: detailview,
					nowrap:true,
					columns:[[
					         {field:'ck',checkbox:true},
							 {field:'rowid' ,align:'center',title:'操作',width:contrlColumn,formatter: function(value, row, index){
								 var re=new RegExp("{param}","g");
								 var ref=opt.replace(re,row.shopid);
								 return ref;
							 }},
							 {field:'shopid',title:'门店编号', align:'left',width:100,sortable:true},
					         {field:'shopname',title:'门店名称', align:'left',width:100,sortable:true},
					         {field:'dealerid',title:'经销商编号', align:'left',width:100,sortable:true},
					         {field:'dealername',title:'经销商名称', align:'left',width:100,sortable:true},
					         {field:'shopaddress',title:'门店地址',align:'left',width:250,formatter:function(value,row,index){
					        	 if(row.shopstreet!=''&&row.shopstreet!=undefined){
					        		 return value+row.shopstreet;
					        	 }
					        	 return value;
					         }},
					         {field:'opendate',title:'开业时间',align:'left',width:80,formatter:function(value,row,index){
					        	 if(value==''){
					        		 return '';
					        	 }
					        	 var datetime=Common.formatterDate(value);
					        	 return datetime;
					         }},
					         {field:'resperson',title:'联系人',align:'left',width:80},
					         {field:'resmobile',title:'联系电话',align:'left',width:80,},
					         {field:'statusname',title:'门店状态',align:'left',width:80},
					         {field:'isflag',title:'状态',align:'left',width:80,formatter:function(value,row,index){
					        	if(value){
					        		return '已停用';
					        	}
					        	else{
					        		return '已启用';
					        	}
					         }},
					         {field:'organization',title:'机构编号',align:'left',width:80},
					         {field:'space',title:'统计间隔',align:'left',width:80},
					         {field:'deposit',title:'押金',align:'left',width:80},
					         {field:'remark',title:'备注',align:'left',width:200,},
					     ]],
			                detailFormatter:function(index,row){
					            return '<div style="padding:2px"><table id="deviceview' + index + '"></table></div>';
			                },
			                onExpandRow:function(index,row){
						    	 var id=row.shopid;
						    	 $("#deviceview"+index).datagrid({
						    		 	url:'/shop/getDevice',
						    		 	queryParams:{'shopid':id},
										title:'设备列表',
										method:'get',
										pagination:true,
										pageList: window.subPageSize,
										loadMsg:'加载中......',
										rowStyler:function(index,row){   
									        if (row.isflag){   
									        	return 'background-color:'+window.flagColor;
									        }   
									    },
									    columns:[[
													 {field:'deviceid',title:'设备编号', align:'left',width:100,sortable:true},
											         {field:'devicename',title:'设备名称', align:'left',width:100,sortable:true},
											         {field:'modelname',title:'设备型号',align:'left',width:250},
											         {field:'deviceprice',title:'设备价格',align:'left',width:80,},
											         {field:'statusname',title:'健康状况',align:'left',width:80,},
											         {field:'isflag',title:'状态',align:'left',width:80,formatter:function(value,row,index){
											        	if(value){
											        		return '已停用';
											        	}
											        	else{
											        		return '已启用';
											        	}
											         }}
											     ]],
									    onResize:function(){  
									    	$('#listShop').datagrid('fixDetailRowHeight',index);  
							             },
						                onLoadSuccess:function(){  
						                    setTimeout(function(){  
						                    	$('#listShop').datagrid('fixDetailRowHeight',index);  
						                    },0);  
						                },
						    	 });
						    	 $('#listShop').datagrid('fixDetailRowHeight',index);
						     }
				});
	$('#listShop').datagrid({   
	    rowStyler:function(index,row){   
	        if (row.isflag){   
	            return 'background-color:'+window.flagColor;   
	        }   
	    }   
	});
	$("#serbutton").click(function(){
		vo.isflag=$("#isflag").val();
		vo.shopid=$("#shopid").val();
		vo.shopname=$("#shopname").val();
		vo.dealerid=$("#dealerid").val();
		vo.dealername=$("#dealername").val();
		vo.resperson=$("#resperson").val();
		vo.regionid=$('#regionid').combotree('getValue');
		$("#listShop").datagrid('load',vo);
	});
	$("#listShop").datagrid({
		toolbar: '#tb'
	});
	$("#regionid").combotree({
  		url:'/region/getRegion',
  		id:'id',
  		text:'text',
  		lines:true,
  		animate:true,
  		editable:true,
  		attributes:'attributes',
  		onBeforeExpand:function(node,param){                         
  			 $('#regionid').combotree("tree").tree("options").url ="/region/getRegion?pid="+node.id;                  
  	    },
//  	    onLoadSuccess:function(node,param){
//  		  var da=[{'id':'',text:'请选择'}];
//  		  $('#regionid').combotree("tree").tree("append",{parent:'',data:da});
//  	    }
//		onClick:function(node){
//			$("#regionid").val(node.id);
//		}
  	});
	//绑定回车事件
	$(document).keydown(function(event){ 
		if(event.keyCode==13){
			$("#serbutton").click(); 
		}
		if(event.keyCode==46){
			var rows=$("#listShop").datagrid('getSelections');
			if(rows.length==1){
				delShop(rows[0].shopid)
			}
			else{
				betchDel()
			}
		}
		if(event.keyCode==67){
			var rows=$("#listShop").datagrid('getSelections');
			if(rows.length<=0){
				$.messager.alert("失败","选中的记录为空：",'error');
			}
			copyShop(rows[0].shopid);
		}
	});
	//如果操作项为空隐藏操作列
	if(opt==''){
		$("#listShop").datagrid('hideColumn','rowid');
	}
});
/**
 * 按状态查询
 * @param status
 */
function searchStatus(status){
	var sta='';
	if(status=="all"){
		$("#all").addClass("a_hover");
		removeClass($("#enable"),"a_hover");
		removeClass($("#disable"),"a_hover");
	}
	if(status==false){
		sta=false;
		$("#enable").addClass("a_hover");
		removeClass($("#all"),"a_hover");
		removeClass($("#disable"),"a_hover");
	}
	if(status==true){
		sta=true;
		removeClass($("#all"),"a_hover");
		removeClass($("#enable"),"a_hover");
		$("#disable").addClass("a_hover");
	}
	$("#isflag").val(sta);
	vo.isflag=sta;
	$("#listShop").datagrid('load',vo);
}
/**
 * 批量启用
 * @param idcard
 */
function betchEnable(){
	var rows=$("#listShop").datagrid('getSelections');
	if(rows.length<=0){
		$.messager.alert('错误','选中的记录为空','error');
		return;
	}
	$.messager.confirm('启用','您确定要启用选中的门店吗？',function(data){
		if(data){
			var ids='';
			for(var i=0;i<rows.length;i++){
				ids+=rows[i].shopid+',';
			}
			$.ajax({
				type:'get',
				url: '/shop/betchEnable',
				data:{ids:ids},
				success:function(data){
					if(data.code==200){
						$.messager.alert("成功","启用成功",'info',function(){
							$("#listShop").datagrid('load',vo);
						});
						
					}
					else{
						$.messager.alert("失败","启用失败："+data.msg,'error');
					}
				},
				error:function(data){
					$.messager.alert("失败","启用失败：",'error');
				}
			});
		}
	})
}
/**
 * 批量停用
 * @param idcard
 */
function betchDisable(){
	var rows=$("#listShop").datagrid('getSelections');
	if(rows.length<=0){
		$.messager.alert('错误','选中的记录为空','error');
		return;
	}
	$.messager.confirm('停用','您确定要停用选中的门店吗？',function(data){
		if(data){
			var ids='';
			for(var i=0;i<rows.length;i++){
				ids+=rows[i].shopid+',';
			}
			$.ajax({
				type:'get',
				url: '/shop/betchDisable',
				data:{ids:ids},
				success:function(data){
					if(data.code==200){
						$.messager.alert("成功","停用成功",'info',function(){
							$("#listShop").datagrid('load',vo);
						});
					}
					else{
						$.messager.alert("失败","停用失败："+data.msg,'error');
					}
				},
				error:function(data){
					$.messager.alert("失败","停用失败：",'error');
				}
			});
		}
	})
}
/**
 * 批量删除
 * @param idcard
 */
function betchDel(){
	var rows=$("#listShop").datagrid('getSelections');
	if(rows.length<=0){
		$.messager.alert('错误','选中的记录为空','error');
		return;
	}
	$.messager.confirm('删除','您确定要删除选中的门店吗？',function(data){
		if(data){
			var ids='';
			for(var i=0;i<rows.length;i++){
				ids+=rows[i].shopid+',';
			}
			$.ajax({
				type:'get',
				url: '/shop/betchDel',
				data:{ids:ids},
				success:function(data){
					if(data.code==200){
						$.messager.alert("成功","删除成功",'info',function(){
							$("#listShop").datagrid('load',vo);
						});
					}
					else{
						$.messager.alert("失败","删除失败："+data.msg,'error');
					}
				},
				error:function(data){
					$.messager.alert("失败","删除失败：",'error');
				}
			});
		}
	})
}
/**
 * 删除
 * @param idcard
 */
function delShop(id){
	$.messager.confirm('删除','您确定要删除选中的门店吗？',function(data){
		if(data){
			$.ajax({
				type:'get',
				url: '/shop/delShop',
				data:{id:id},
				success:function(data){
					if(data.code==200){
						$.messager.alert("成功","删除成功",'info',function(){
							$("#listShop").datagrid('load',vo);
						});
					}
					else{
						$.messager.alert("失败","删除失败："+data.msg,'error');
					}
				},
				error:function(data){
					$.messager.alert("失败","删除失败：",'error');
				}
			});
		}
	})
}
/**
 * 详情
 */
function viewShop(id){
	window.parent.openDialog("门店详情",'/shop/viewShop','',430,630,'id='+id);
}
/**
 * 新增
 */
function addShop(){
	window.parent.openDialog("新增门店",'/shop/addShop','',400,620,'');
}
/**
 * 复制
 */
function copyShop(id){
	window.parent.openDialog("新增门店",'/shop/copyShop','',400,620,'id='+id);
}
/**
 * 编辑
 * @param id
 */
function edit(id){
	window.parent.openDialog("修改门店",'/shop/editShop','',400,600,'id='+id);
}
/**
 * 导出为excel
 */
function exportXls(){
	vo.dealerid=getVal(vo.dealerid);
	vo.dealername=getVal(vo.dealername);
	vo.resperson=getVal(vo.resperson);
	vo.isflag=getVal(vo.isflag);
	vo.shopid=getVal(vo.shopid);
	vo.shopname=getVal(vo.shopname);
	var url='/shop/exportXls?dealerid='+vo.dealerid+'&dealername='+vo.dealername+'&resperson='+
	vo.resperson+'&isflag='+vo.isflag+'&shopid='+vo.shopid+'&shopname='+vo.shopname;
	window.open(url,'','',false);
}
